package com.youlian.cloud.log.filter;


import com.youlian.cloud.log.utils.EventProcessUtils;
import com.youlian.cloud.log.utils.RequestUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 拦截请求中的信息
 *
 * @author : liukx
 * @date : 2019/1/25 - 10:24
 */
public class CatHttpRequestFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        // 获取客户端编号
        String remoteAddr = RequestUtils.getClientIP(req);
        // 获取客户使用设备
        String userAgent = RequestUtils.getUserAgent(req);

        /**
         * 从用户代理设备中解析设备信息
         */
//        if (StringUtils.isNotEmpty(userAgent)) {
            // todo 需要统一确认各端传递过来的信息

//        }

        // 获取客户端跳转过来的前端域名
        String referer = RequestUtils.getReferer(req);
        // 将捕获的请求打印到CAT中方便查看
        EventProcessUtils.write("filter-userAgent", userAgent);
        EventProcessUtils.write("filter-referer", referer);
        EventProcessUtils.write("filter-clientIp", remoteAddr);
        chain.doFilter(request, response);

    }


    @Override
    public void destroy() {

    }
}
